package com.wdz.kafka;

import cn.hutool.core.convert.Convert;
import cn.hutool.extra.mail.MailUtil;
import com.wdz.kafka.constant.Topics;
import com.wdz.kafka.entity.EmailEntity;
import com.wdz.tofs.tofs_menu.entity.TofsMenu;
import com.wdz.tofs.tofs_menu.service.ITofsMenuService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * Kafka消费者
 *
 */
@Component
public class Consumer {

    //@KafkaListener(topics = Topics.EMAILS,groupId = "topic-hsiu1gfw-emails")
    public void consumer(ConsumerRecord<String,Object> record){
        Optional<Object> optional = Optional.ofNullable(record.value());
        if(optional.isPresent()){
            //o对象就是我们接收到要消费的消息
            //o对象已经使用配置文件中配置反序列化类反序列化了
            //Object o = optional.get();
            System.out.println(record.value());
        }
    }
    private final static String groupId = "consumer1";

    @KafkaListener(topics = Topics.EMAILS,groupId = groupId)
    public void consumerEmail(EmailEntity emailEntity){
        MailUtil.send(emailEntity.getToEmail(), emailEntity.getTitle(),
                emailEntity.getContent(), true);
    }

}
